package com.yanh.transaction.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.yanh.common.util.QRCodeUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

@Slf4j
@Controller
@RequestMapping("/pay-test")
public class PayTestController {

    //应用id
    String APP_ID = "9021000138609267";
    //应用私钥
    String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCnzDrQ0MRVacLHIZzD4s++kt9ysC3fYacW5+d5FBe6Bd8Or45Ouv5mUslG5w12No9xDAjBkzkhJKacEB3GPPamT1NEBZYB0nQ64+H4E5IU34VnwvNnSkMEFRibEOlpDocG9q1WLLS9qrZwE9vpJ5MyqzLhHDp/EZbPmXHIMA/weaJEWoVbYqoTlqXzi3Hd/aLsJz+bF5+dkszW3O7EFAAbJyMoOa1B6kprFeMUNz5aXDzrW8CS1nVtEWJSMH1kv9/1XaTiqjCAEzuRYNHBGnmcJekgaKIfr1kFG93o4/oHg5lydAYZixu9CXqt5gmlUQKu1s2BN9Blj96663hMapCtAgMBAAECggEAYEOcYnvChzXblp5D72VJxY1BP+yvBH5je5KjiyTpz8DajPROcgB5Pn60Fkcrw+vdMv+ybktl9QuvwczFKBuFbTgJDvcdbgIsyDRASTABGJR7VmHTfihudgMcUKnNHoZFFMuj+dJABqfPblEWUpZvrhvlXpvxO0D+Ai+HxU89dbl/Phx9nO/lJDiUL4ZpqxYwiYGqVcyVZmL/2yR91BJ+v+ns6b+aIn2VkpYMW1zRzR8w16/PrBpHrgJLdj7DEemX/SkenEBKTeWpZDWHMJg5AmupMZzvsTOE/JtV2pjwfnBK+4e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i6AWrQhzM3au8gFAG8f7TVOZzMpteXaMgbOSoJ/0kifQ9HxHvMIIbzAPsihnU0IwKPNy9tEC9xc7fDd5BH5Zk8rTUc9rorRD1uFCRd5LYhMCbp9R5c79muwQRdWDxn7zeUaqR6q+Gp6f2DLPo6aNmdeUrfWQ+KIt3sisHUz1PMRjkMwBWCuCNSYOXJbJqZjNAqiUWUPT3es5sAMFfXgluhD2gXWxfE6zvoZtOC3QIpHRSCndo28c/yGKtP692xRw6lHhwIDAQAB";
    String CHARSET = "utf-8";
    //支付宝接口的网关地址，正式"https://openapi.alipay.com/gateway.do"
    String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    //签名算法类型
    String sign_type = "RSA2";

    @GetMapping("/qrCode")
    public  void qrCode() throws IOException {
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        String qrCode = qrCodeUtil.createQRCode("http://192.168.78.99:8182/pay-test/alipaytest", 200, 200);
        System.out.println(qrCode);
    }

    @GetMapping("/alipaytest")
    public void alipaytest(HttpServletRequest httpRequest,
                           HttpServletResponse httpResponse) throws ServletException, IOException {
        //构造sdk的客户端对象
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, sign_type); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
//        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
//        alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\"20150420010101033\"," +
                " \"total_amount\":\"88.88\"," +
                " \"subject\":\"Iphone6 16G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数
        String form="";
        try {
            //请求支付宝下单接口,发起http请求
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
}